package com.ranjiaqi.output;

/**
 * @ClassName TestOutputStream
 * @Description TODO
 * @Author hp
 * @Date 2021/6/30 9:30
 * @Version 1.0
 **/

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * 字节输出流，写入文件
 * 子类：文件字节输出流，FileOutputStream
 * 流对象操作文件的步骤：
 *  1.创建流对象
 *  2.流对象的方法操作文件
 *  3.释放对象
 */
public class TestOutputStream {
    public static void main(String[] args) throws IOException {
//        writeByte();
//        writeByteArray();
//        writeAppend();

    }


    public  static void writeAppend() throws IOException {
        FileOutputStream fileOutputStream = new FileOutputStream("day20/2.txt", true);
        byte[] bytes = "今天你又犯困了嘛？\r\n".getBytes();
        byte[] bytes1 = "困了".getBytes();
        fileOutputStream.write(bytes);
        fileOutputStream.write(bytes1);
        fileOutputStream.close();
    }

    /**
     * 释放资源，流对象的方法close()
     * JVM:本身没有IO流的功能
     * 任何操作系统都天然具备IO，调用操作系统的功能实现
     * 使用完操作系统的功能，释放资源
     */
    public static void close() throws IOException {
        OutputStream fileOutputStream = new FileOutputStream("D:/1.txt");
        fileOutputStream.write(100);
        fileOutputStream.close();
    }


    public static void writeByte() throws IOException {
        OutputStream fileOutputStream = new FileOutputStream("D:/1.txt");
        //
        /*fileOutputStream.write(100);
        fileOutputStream.write(101);*/
        fileOutputStream.write(49);
        fileOutputStream.write(48);
        fileOutputStream.write(48);
        fileOutputStream.close();

    }

    /**
     * write(byte[] b) 写入字节数组
     * 文件出现在day20模块
     */
    public static void  writeByteArray() throws IOException {
        //创建流对象，构造方法传递字符串的文件名
        FileOutputStream fos = new FileOutputStream("day20/1.txt");
        byte[] bytes = "abcdefwa".getBytes();
        //输出流的方法写入数组
        fos.write(bytes);
        fos.write(bytes,0,2);
        fos.close();

    }
}
